package cn.wolfcode.web.controller;


import cn.wolfcode.domain.Result;
import cn.wolfcode.util.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.UUID;

@Slf4j
@RestController
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;
    @PostMapping("/upload")
    public Result upload(MultipartFile image) throws IOException {
        log.info("文件上传:{}",image);
//        /**
//         * 如果是上传到本地
//         * 如果是上传到云:
//         * 把文件存储到指定为止
//         * 响应的地址给浏览器
//         *
//         */
//        String originalFilename = image.getOriginalFilename();
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String uuid= UUID.randomUUID().toString();
//        System.out.println("originalFilename = " + originalFilename);
//        //将文件存储在本地服务中
//
//        String newFileName=uuid+substring;
//        URL resource = this.getClass().getClassLoader().getResource("static/");
//
//        String path = resource.getPath();
//        System.out.println("path = "+path);
//        File file=new File(path+newFileName);
//        image.transferTo(file);
//        return Result.success("api/"+newFileName);
        return Result.success(aliOSSUtils.upload(image));
    }
}
